Msg("====Commands module initialized...\n")

gSAM.Commands = {}
gSAM.CMDs = {}

function gSAM.RegisterCommand(rname,cname,func,level,admin)
	Msg("registering command: "..rname.." : "..cname.."\n")
	local cmd = {}
	cmd.Name = rname
	cmd.ChatName = cname
	cmd.Function = func
	cmd.AccessLevel = level or 9
	cmd.IsAdmin = admin or 0
	table.insert(gSAM.CMDs,cmd)
	cmd = {}
end

function gSAM.LoadCommands()
	for k,v in pairs(file.FindInLua("gSAM/Server/Commands/*.lua")) do
		include("gSAM/Server/Commands/"..v)
		Msg("====gSAM Command Module: "..v.." Included...\n")
	end
end
hook.Add("Initialize","gSAM.LoadCommands",gSAM.LoadCommands)

function gSAM.PlayerSay(ply,txt)
	local text = string.Explode(" ",txt)
	for k,v in pairs(gSAM.CMDs) do
		if string.lower(text[1]) == string.lower(gSAM.Settings.ChatPrefix..v.ChatName) then
			if v.IsAdmin == 1 then
				if gSAM.GetPlayerGroup(ply) <= v.AccessLevel then
					if ply:IsAdmin() then
						local args = text
						table.remove(args,1)
						return v.Function(ply,v.Name,args) or ""
					else
						gSAM.HintPlayer(ply,"You have to be an Admin to use this command!")
						return ""
					end
				else
					gSAM.HintPlayer(ply,"You don't have the required access level!")
					return ""
				end
			elseif v.IsAdmin == 2 then
				if ply:IsSuperAdmin() then
					local args = text
					table.remove(args,1)
					return v.Function(ply,v.Name,args) or ""
				else
					gSAM.HintPlayer(ply,"You have to be a Super-Admin to use this command!")
					return ""
				end
			end
			if gSAM.GetPlayerGroup(ply) <= v.AccessLevel then
				local args = text
				table.remove(args,1)
				return v.Function(ply,v.Name,args) or ""
			else
				gSAM.HintPlayer(ply,"You don't have the required access level!")
			end
		end
	end
end
hook.Add("PlayerSay","gSAM.PlayerSay",gSAM.PlayerSay)
